<!DOCTYPE html>
<body>
    <head>
        <meta charset="utf-8">
        <title>自定义对话框预览</title>
        <#include "/pubHeadStyleTree.html">
        <script src="${ctx}/formStatic/form/CommonDialog.js"></script>
    </head>
    <body>
<div class="ccpui-tablesize" style="padding:10px;position: relative">
    <table id="formDialog" lay-filter="formDialog"></table>
</div>
<#if bpmFormDialog.style==1>
<div id="dialogTree" class="ztree"></div>
<div style="position: absolute;bottom: 15px;left: 29%;">
    <a class="layui-btn layui-btn-sm" id="queryTreeData">
        <span>确定</span>
    </a>
    <a class="layui-btn layui-btn-sm" id="reset">
        <span>重置</span>
    </a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" id="cancel">
        <span>取消</span>
    </a>
</div>
<#else>
<script type="text/html" id="formDialogToolsBar">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <div style="position: absolute;bottom:-44px;left:40%">
            <a class="layui-btn layui-btn-sm" lay-event="determineQuery">
                <span>确定</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="reset">
                <span>重置</span>
            </a>
            <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="cancel">
                <span>取消</span>
            </a>
        </div>
    </div>
    <form class="layui-form" action="" style="margin-top:10px" lay-filter="formDialog_query" onsubmit="return false">
        <div class="table-top-filter">
            <#if bpmFormDialog.conditionList?? && (bpmFormDialog.conditionList?size > 0) >
            <#list bpmFormDialog.conditionList as item>
            <#if item.defaultType==1>
            <div class="table-filter-alone">
                <label class="layui-form-label outofHiding" title="${item.comment }">${item.comment }</label>
                <#if item.fieldType=='date'>
                <#if item.condition=='='>
                <input type="text" name="Q_${item.fieldName}_DL" class="layui-input start_time filter-value"/>
                <#elseif item.condition=='>='>
                <input type="text" name="Q_start${item.fieldName}_DL" class="layui-input start_time filter-value"/>
                <#elseif item.condition=='<='>
                <input type="text" name="Q_end${item.fieldName}_DG" class="layui-input end_time filter-value"/>
                <#else>
                <input type="text" name="Q_start${item.fieldName}_DL"
                       class="layui-input filter-value start_time"/>
                <label class="layui-form-label">到:</label>
                <input type="text" name="Q_end${item.fieldName}_DG" class="layui-input filter-value end_time"/>
            </#if>
            <#elseif item.fieldType=='number'>
            <input type="text" name="Q_${item.fieldName}_DB" class="layui-input"/>
            <#else>
            <#if item.paraCt=='1'>
            <input type="text" name="${item.fieldName }" class="layui-input filter-value"/>
        </#if>
    </#if>
    <#if item.fieldType!='date'>
    <#if item.paraCt=='0'>
    <input type="hidden" name="selectIds">
    <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
    <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectCustomDialog(this);"id="dialog" dialog="${item.dialog}" param="${item.param}" style="margin: 4px;width:48px;">
    <#elseif item.paraCt=='4'>
    <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
    <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectUser(this);" style="margin: 4px;width:48px;">
    <#elseif item.paraCt=='17'>
    <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
    <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectRole(this);" style="margin: 4px;width:48px;">
    <#elseif item.paraCt=='18'>
    <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
    <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectOrg(this);" style="margin: 4px;width:48px;">
    <#elseif item.paraCt=='19'> <!--岗位-->
    <input type="text" readonly="readonly" class="layui-input filter-value" name="${item.fieldName}">
    <input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="selectUser(this);" style="margin: 4px;width:48px;">
    </#if>
    </div>
    </#if> </#if>
    </#list>
    </#if>
    <input type="hidden" name="dialog_alias_" value="${bpmFormDialog.alias}">
    </div>
    </form>
</script>
</#if>
<textarea id="txtJsonData" style="display:none"></textarea>
<input style="display:none" id="parentIndex"/>
<script>
    var needPage = true;
    if ('${bpmFormDialog.needpage}' == 0) {
        needPage = false;
    }
    var setter;
    var isOpenJsonData = '${isOpenJsonData}';
    //已选择的记录
    layui.selectData = null;
        layui.config({
            base: serverPath.formPath + staticPath.formPath //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
        }).use(['index', 'commons','table', 'layer', 'jquery', 'form', 'tree', 'common', 'totalFilter', 'setter', 'code'], function () {
        var table = layui.table, layer = layui.layer, commons = layui.commons,
            common = layui.common, totalFilter = layui.totalFilter, form = layui.form, code = layui.code;
        common.tableSort("formDialog");
        layui.code();
        setter = layui.setter;
        if ('${bpmFormDialog.style}' ==='0') {
            //组装列表显示字段数据
            var displayField = JSON.parse('${bpmFormDialog.displayfield}');
            var sortfield = JSON.parse('${bpmFormDialog.sortfield}');
            var isSingle = '${bpmFormDialog.issingle}';
            var array = [];
            var checkboxField = {type: 'checkbox', fixed: 'left'};
            if (isSingle === '1') {
                checkboxField = {type: 'radio', fixed: 'left'};
            }
            array.push(checkboxField);
            for (var i = 0; i < displayField.length; i++) {
                var json = {};
                var fieldName = displayField[i].field;
                json.field = fieldName;
                json.title = displayField[i].comment;
                json.align = "center";
                for (var k = 0; k < sortfield.length; k++) {
                    if (sortfield[k].field == fieldName){
                        json.sort = true;
                    }
                }
                array.push(json);
            }

            //渲染表格
            table.render({
                elem: '#formDialog'
                , cellMinWidth: 80
                , title: '自定义对话框表'
                , defaultToolbar: []
                , height: 'full-75'
                , url: serverPath.formPath + setter.formLinkPath + 'formDialog/showTableData?alias=' + '${alias}'+'${paramStr}'
                , toolbar: '#formDialogToolsBar'
                , page: {limit: '${bpmFormDialog.pagesize}', page: needPage}
                , limits: [5, 10, 15, 20, 50]
                , limit: '${bpmFormDialog.pagesize}'
                , autoSort: false
                , cols: [array]
            });
            totalFilter.init('formDialog');

            table.on('toolbar(formDialog)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data; //获取选中行数据
                switch (obj.event) {
                    case 'query':
                        common.tableConditionRender("formDialog", "formDialog_query", {where: {}}, false,false,'${bpmFormDialog.pagesize}');
                        commons.reflashDate({startName: '.start_time', endName: '.end_time'});
                        break;
                    case 'determineQuery':
                        if(getRadioCheckCount()<=0) {
                            layer.alert("还没有选择数据项！");
                            return false;
                        }
                        $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
                        var isSingle = '${bpmFormDialog.issingle}';
                        var resultField = JSON.parse('${bpmFormDialog.resultfield}');
                        if(isSingle === '0') { //多选
                            var returnData = [];
                            for (var k= 0; k < data.length; k++) {
                                var resultJson = {};
                                for (var j = 0; j < resultField.length; j++) {
                                    var returnField = resultField[j].field;
                                    resultJson[returnField] = data[k][returnField];
                                    if(j == resultField.length-1){
                                        returnData.push(resultJson);
                                    }
                                }
                            }
                            $("#txtJsonData").val(JSON.stringify(returnData));
                        }else{
                            var resultJson = {};
                            for (var p = 0; p < resultField.length; p++) {
                                resultJson[resultField[p].field] = data[0][resultField[p].field];
                            }
                            $("#txtJsonData").val(JSON.stringify(resultJson));
                        }
                        layui.selectData= $("#txtJsonData").val();
                        if(isOpenJsonData){
                            openQueryJsonDialog();
                        }else{
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                        break;
                    case 'reset':
                        var radios =  $("input[name^='layTableRadio_']");
                        radios.each(function () {
                            $(this).prop("checked", false);
                        });
                        var item =  $("input[name='layTableCheckbox']");
                        item.each(function () {
                            $(this).prop("checked", false);
                        });
                        form.render('radio');
                        form.render('checkbox');
                        data=[];
                        break;
                    case 'cancel':
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        break;
                }
            });

            window.Initialization = function () {
                commons.reflashDate({startName: '.start_time', endName: '.end_time'});
            };
            /*刷新时间控件*/
            commons.reflashDate({startName: '.start_time', endName: '.end_time'});
            common.tableSort("formDialog", {show: window.Initialization});

            //获取选中数量
            function getRadioCheckCount(){
                var count=0;
                var isSingle = '${bpmFormDialog.issingle}';
                if(isSingle === '1') {
                    var radios= $("input[lay-type='layTableRadio']");
                    radios.each(function () {
                        if ($(this).prop("checked") === true) {
                            count++
                        }
                    });
                }else{
                    var item =  $("input[name='layTableCheckbox']");
                    item.each(function () {
                        if ($(this).prop("checked") === true) {
                            count++
                        }
                    });
                }
                return count;
            }
        }

        //树的处理开始====
        var zTree;
        var json=JSON.parse('${bpmFormDialog.displayfield}');
        var isSingle = '${bpmFormDialog.issingle}';
        var radioCheckboxFlag = false;
        if (isSingle === '0') {
            radioCheckboxFlag = true;
        }
        if ('${bpmFormDialog.style}' === '1') {
            loadTree();
        }
        //加载树
        function loadTree() {
            var setting = {
                async:{
                    enable:true,
                    url:serverPath.formPath + setter.formLinkPath + 'formDialog/getTreeData?alias='+'${alias}'+'&isRoot=1',
                    autoParam:[json.id+"=idKey",json.pid+"=pidKey",json.displayName+"=nameKey"],
                    otherParam:{idKeyName:json.id,pidKeyName:json.pid,nameKeyName:json.displayName}
                },
                data: {
                    key : {name: json.displayName},
                    simpleData: {
                        enable: true,
                        idKey: json.id,
                        pIdKey: json.pid
                    }
                },
                check: {
                    enable: radioCheckboxFlag
                },
                callback: {
                    onClick: getClickData
                }
            };
            zTree=$.fn.zTree.init($('#dialogTree'), setting);
        }
        //取消
        $("#cancel").click(function(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        //重置
        $("#reset").click(function(){
            zTree.checkAllNodes(false);
        });

        //返回字段值
        var resultField = JSON.parse('${bpmFormDialog.resultfield}');
        var resultSingleJson={};
        //树节点单击事件
        function getClickData(event, treeId, treeNode) {
            for(var j=0;j<resultField.length;j++){
                var returnField = resultField[j].field;
                resultSingleJson[returnField]=treeNode[returnField];
            }
            $("#txtJsonData").val(JSON.stringify(resultSingleJson));
        }
        //确定
        $("#queryTreeData").click(function(){
            if(radioCheckboxFlag){ //多选
                var returnData=[];
                var treeObj=$.fn.zTree.getZTreeObj("dialogTree"),
                    nodes=treeObj.getCheckedNodes(true);
                for(var i=0;i<nodes.length;i++){
                    if(!nodes[i].getCheckStatus().half){
                        var resultJson={};
                        for(var j=0;j<resultField.length;j++){
                            var returnField = resultField[j].field;
                            resultJson[returnField]=nodes[i][returnField];
                        }
                        returnData.push(resultJson);
                    }
                }
                if(JSON.stringify(returnData)==="[]") {
                    layer.alert("还没有选择数据项！");
                    return false;
                }
                $("#txtJsonData").val(JSON.stringify(returnData));
            }else{
                if(JSON.stringify(resultSingleJson)==="{}") {
                    layer.alert("还没有选择数据项！");
                    return false;
                }
            }
            layui.selectData= $("#txtJsonData").val();
            if(isOpenJsonData){
                openQueryJsonDialog();
            }else{
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
            $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
        });

        //打开json数据弹框
        function openQueryJsonDialog(){
            parent.layer.open({
                type: 2,
                area: ["550px",'375px'],
                title: ['提示'],
                content: serverPath.formPath + setter.formLinkPath + "formDialog/view/showJson?parentName="+window.name,
                success: function (layero) {
                    var mask = $(".layui-layer-shade");
                    mask.appendTo(layero.parent());
                }
            });
        }
    });
</script>
<style>
    .layui-table-tool {
        position: inherit;
    }
    #LAY_app{
        position: relative;
    }
    .outofHiding{
        width: 141px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
</style>
</body>
</html>
